package com.ymatou.iapi.fenqile.testcase;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.fenqile.parameter.SyncFqlAccessTokenBean;
import com.ymatou.iapi.fenqile.service.SyncFqlAccessTokenCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.PayGateWayCallService;
import com.ymttest.database.model.FqlMember;
import com.ymttest.database.model.FqlMemberExample;
import com.ymttest.database.sqlwapper.PayGateWayWapper;
/**
*分期乐同步token信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SyncFqlAccessToken{
	private static SyncFqlAccessTokenBean syncfqlaccesstokenBean;
	private static SyncFqlAccessTokenCall syncfqlaccesstokenCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("分期乐同步token信息");
	}

	@Before
	public void caseUp() {
		syncfqlaccesstokenBean=new SyncFqlAccessTokenBean();
		syncfqlaccesstokenCall=new SyncFqlAccessTokenCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(syncfqlaccesstokenCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SyncFqlAccessToken_001() {
		Logger.start(true,"分期乐同步token信息");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token=registerResult.get("Token").getAsString();
			int uid  = urrc.getUserid();
			//获取openid
			Map mcode = PayGateWayCallService.CodeFin(uid);
			Thread.sleep(3000);
			FqlMemberExample example=new FqlMemberExample();
			example.createCriteria().andUserIdEqualTo(uid);
			List<FqlMember> dbs = new PayGateWayWapper().selectFqlMemberExample(example);
			Logger.verifyEquals(1, dbs.size(), "验证数据进入数据库");
			Logger.debug(FormatUtil.beanToGSONString(dbs));
			Logger.verifyEquals(mcode.get("MockResult-FqlPay-open_id"), dbs.get(0).getFqlOpenId(), "验证FqlOpenId");
			Logger.verifyEquals(mcode.get("MockResult-FqlPay-refresh_token"), dbs.get(0).getRefreshToken(), "验证RefreshToken");
			Logger.verifyEquals(mcode.get("MockResult-FqlPay-access_token"), dbs.get(0).getAccessToken(), "验证AccessToken");
			
			HashMap mockHeader=(HashMap) PayGateWayCallService.CodeMockMap();
			syncfqlaccesstokenBean.setUserId(uid);
			PayGateWayCallService.addMock(syncfqlaccesstokenCall, mockHeader);
			syncfqlaccesstokenCall.setData(syncfqlaccesstokenBean);
			syncfqlaccesstokenCall.callService();			
			
			dbs = new PayGateWayWapper().selectFqlMemberExample(example);
			Logger.verifyEquals(1, dbs.size(), "验证数据进入数据库");
			Logger.debug(FormatUtil.beanToGSONString(dbs));
			Logger.verifyEquals(mockHeader.get("MockResult-FqlPay-refresh_token"), dbs.get(0).getRefreshToken(), "验证RefreshToken");
			Logger.verifyEquals(mockHeader.get("MockResult-FqlPay-access_token"), dbs.get(0).getAccessToken(), "验证AccessToken");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SyncFqlAccessToken_002() {
		Logger.start(true,"openid对应的uid已注销同步分期乐用户信息成功");
		//"result": "56002008"
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token=registerResult.get("Token").getAsString();
			int uid  = urrc.getUserid();
			//获取openid
			Map mcode = PayGateWayCallService.CodeFin(uid);
			Thread.sleep(3000);
			FqlMemberExample example=new FqlMemberExample();
			example.createCriteria().andUserIdEqualTo(uid);
			List<FqlMember> dbs = new PayGateWayWapper().selectFqlMemberExample(example);
			Logger.verifyEquals(1, dbs.size(), "验证数据进入数据库");
			Logger.debug(FormatUtil.beanToGSONString(dbs));
			Logger.verifyEquals(mcode.get("MockResult-FqlPay-open_id"), dbs.get(0).getFqlOpenId(), "验证FqlOpenId");
			Logger.verifyEquals(mcode.get("MockResult-FqlPay-refresh_token"), dbs.get(0).getRefreshToken(), "验证RefreshToken");
			Logger.verifyEquals(mcode.get("MockResult-FqlPay-access_token"), dbs.get(0).getAccessToken(), "验证AccessToken");
			
			HashMap mockHeader=(HashMap) PayGateWayCallService.CodeMockMap();
			mockHeader.put("MockResult-FqlPay-rows_result", "56002008");
			syncfqlaccesstokenBean.setUserId(uid);
			PayGateWayCallService.addMock(syncfqlaccesstokenCall, mockHeader);
			syncfqlaccesstokenCall.setData(syncfqlaccesstokenBean);
			syncfqlaccesstokenCall.callService();			
			
			dbs = new PayGateWayWapper().selectFqlMemberExample(example);
			Logger.verifyEquals(1, dbs.size(), "验证数据进入数据库");
			Logger.debug(FormatUtil.beanToGSONString(dbs));
			//因为失败了所以不更新token
			Logger.verifyEquals(mcode.get("MockResult-FqlPay-refresh_token"), dbs.get(0).getRefreshToken(), "验证RefreshToken");
			Logger.verifyEquals(mcode.get("MockResult-FqlPay-access_token"), dbs.get(0).getAccessToken(), "验证AccessToken");
			Logger.verifyEquals(3, dbs.get(0).getState(), "验证FqlMemberStatus");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}